package com.zhs.gccs.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.zhs.gccs.R;
import com.zhs.gccs.base.BaseActivity;
import com.zhs.gccs.model.PointInfo;
import com.zhs.gccs.dao.PointInfoDao;
import com.zhs.gccs.model.PointBean;
import com.zhs.gccs.utils.ToastUtils;
import com.orhanobut.logger.Logger;

public class ItemPointDetail extends LinearLayout {
    public ItemPointDetail(Context context) {
        super(context);
        init(context);
    }

    public ItemPointDetail(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ItemPointDetail(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private Context context;
    private TextView item_task_point_item_tv1;
    private TextView item_task_point_item_tv2;
    private Switch item_project_point_is_no;

    private ImageView item_task_point_item_iv31;
    private ImageView item_task_point_item_iv32;
    private ImageView item_task_point_item_iv4;

    private ImageView item_task_point_item_iv41;
    private ImageView item_task_point_item_iv42;
    private ImageView item_task_point_item_iv43;

    private EditText item_task_point_item_et5;
    private TextView item_task_point_item_6;
    private PointBean pointBean;
    private int isOrNot = -1;//-1没有选  0否 1是
    private PointInfoDao pointInfoDao;
    private String bsm;
    private PointInfo pointInfo = new PointInfo();

    private ItemTaskPointListener itemTaskPointListener;

    public void setItemTaskPointListener(ItemTaskPointListener itemTaskPointListener) {
        this.itemTaskPointListener = itemTaskPointListener;
    }

    private void init(Context context) {
        pointInfoDao = new PointInfoDao();
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.item_task_point, null);
        addView(view);

        LinearLayout.LayoutParams params = (LayoutParams) view.getLayoutParams();
        params.width = (int) (BaseActivity.displayWidth * 0.41);
        params.height = BaseActivity.MATCH_PARENT;
        params.rightMargin = (int) (10 * BaseActivity.density);
        params.topMargin = (int) (10 * BaseActivity.density);
        params.bottomMargin = (int) (10 * BaseActivity.density);

        view.setLayoutParams(params);

        ImageView close = view.findViewById(R.id.item_project_detail_close);
        close.setOnClickListener(view1 -> {
            closeListener.onClose(2);
//            setVisibility(GONE);
        });

//        pointBean = new PointBean();
//        pointBean.setAttribute("井深2m");
//        pointBean.setPointName("灌溉机井1核查信息");

        /////
        item_task_point_item_tv1 = view.findViewById(R.id.item_task_point_item_tv1);
        item_task_point_item_tv2 = view.findViewById(R.id.item_task_point_item_tv2);

        item_project_point_is_no = view.findViewById(R.id.item_project_point_is_no);

        item_task_point_item_iv4 = view.findViewById(R.id.item_task_point_item_iv4);
        item_task_point_item_iv41 = view.findViewById(R.id.item_task_point_item_iv41);
        item_task_point_item_iv42 = view.findViewById(R.id.item_task_point_item_iv42);
        item_task_point_item_iv43 = view.findViewById(R.id.item_task_point_item_iv43);
        item_task_point_item_et5 = view.findViewById(R.id.item_task_point_item_et5);//说明
        item_task_point_item_6 = view.findViewById(R.id.item_task_point_item_6);


        item_project_point_is_no.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    pointInfo.setIsExist(1);
                }else{
                    pointInfo.setIsExist(0);
                }
            }
        });
        item_task_point_item_iv4.setOnClickListener(view14 -> {
            int position = -1;
            if (pointBean != null) {
                String imagePath1 = pointBean.getImagePath1();
                String imagePath2 = pointBean.getImagePath2();
                String imagePath3 = pointBean.getImagePath3();
                if (TextUtils.isEmpty(imagePath1)) {
                    position = 0;
//                    item_task_point_item_iv41.setVisibility(VISIBLE);
                } else if (TextUtils.isEmpty(imagePath2)) {
                    position = 1;
//                    item_task_point_item_iv42.setVisibility(VISIBLE);
                } else if (TextUtils.isEmpty(imagePath3)) {
                    position = 2;
//                    item_task_point_item_iv43.setVisibility(VISIBLE);
                }
            }

            //三个图片都拍了 -1
            if (position != -1) {
                if (itemTaskPointListener != null) {
                    itemTaskPointListener.takePhoto(position,item_project_point_is_no.isChecked());
                }
            }

        });

        item_task_point_item_iv41.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = pointBean.getImagePath1();
                imageClickListener.onClick(path);
            }
        });
        item_task_point_item_iv41.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                item_task_point_item_iv41.setVisibility(GONE);
                pointBean.setImagePath1(null);
                imageDeleteListener.onImageDelete(1);
                item_task_point_item_iv4.setVisibility(VISIBLE);
                return true;
            }
        });
        item_task_point_item_iv42.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = pointBean.getImagePath2();
                imageClickListener.onClick(path);
            }
        });
        item_task_point_item_iv42.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                item_task_point_item_iv42.setVisibility(GONE);
                pointBean.setImagePath2(null);
                imageDeleteListener.onImageDelete(2);
                item_task_point_item_iv4.setVisibility(VISIBLE);
                return true;
            }
        });
        item_task_point_item_iv43.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = pointBean.getImagePath3();
                imageClickListener.onClick(path);
            }
        });
        item_task_point_item_iv43.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                item_task_point_item_iv4.setVisibility(VISIBLE);
                item_task_point_item_iv43.setVisibility(GONE);
                pointBean.setImagePath3(null);
                imageDeleteListener.onImageDelete(3);
                return true;
            }
        });

        item_task_point_item_et5.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s != null && pointBean != null && pointInfo != null){
                    pointInfo.setReadme(s.toString());
                    pointBean.setReadme(s.toString());
                }
            }
        });

        item_task_point_item_6.setOnClickListener(view15 -> {
            Logger.i("保存");
            //保存
            String imagePath1 = pointInfo.getImagePath1();
            String imagePath2 = pointInfo.getImagePath2();
            String imagePath3 = pointInfo.getImagePath3();
            if(TextUtils.isEmpty(imagePath1) ||
                    TextUtils.isEmpty(imagePath2) ||
                    TextUtils.isEmpty(imagePath3)){
                ToastUtils.toast("请拍摄3张图片");
            }else{
                pointInfo.setIsCheck(1);
                pointInfoDao.savePointInfo(pointInfo);
                setVisibility(GONE);
                commitListener.onCommit();
            }
        });

    }

    public void setPointBean(PointBean pointBean) {
        this.pointBean = pointBean;
        bsm = pointBean.getPointId();
        pointInfo.setPointId(bsm);
        pointInfo.setpId(pointBean.getpId());
        pointInfo.setPointName(pointBean.getPointName());
        pointInfo.setAttribute(pointBean.getAttribute());
        pointInfo.setImagePath1(pointBean.getImagePath1());
        pointInfo.setImagePath2(pointBean.getImagePath2());
        pointInfo.setImagePath3(pointBean.getImagePath3());
        pointInfo.setIsCheck(pointBean.getCheckState());
        pointInfo.setIsExist(pointBean.getIsExist());
        Logger.i("设置setPointBean");
//        pointInfoDao.savePointInfo(pointInfo);

        item_task_point_item_tv1.setText(pointBean.getPointName());
        item_task_point_item_tv2.setText(pointBean.getAttribute());

        int isExist = pointBean.getIsExist();//-1没有选  0否 1是

//        if (isExist == 1) {
//            //是
//            item_task_point_item_iv31.setImageResource(R.mipmap.radio_checked);
//            item_task_point_item_iv32.setImageResource(R.mipmap.radio_normal);
//        } else if (isExist == 0) {
//            //否
//            item_task_point_item_iv32.setImageResource(R.mipmap.radio_checked);
//            item_task_point_item_iv31.setImageResource(R.mipmap.radio_normal);
//        } else if (isExist == -1) {
//            //-1没有选
//            item_task_point_item_iv31.setImageResource(R.mipmap.radio_normal);
//            item_task_point_item_iv32.setImageResource(R.mipmap.radio_normal);
//        }

        if(pointInfo.getIsExist() == 0){
            item_project_point_is_no.setChecked(false);
        }else{
            item_project_point_is_no.setChecked(true);
        }
        //说明
        item_task_point_item_et5.setText(pointBean.getReadme());

//        if(pointBean.getCheckState() == 1){
//            item_task_point_item_6.setVisibility(INVISIBLE);
//        }else{
//            item_task_point_item_6.setVisibility(VISIBLE);
//        }

        if(!TextUtils.isEmpty(pointBean.getImagePath1())){
            item_task_point_item_iv41.setVisibility(VISIBLE);
            Glide.with(context)
                    .load(pointBean.getImagePath1())
                    .centerCrop()
                    .thumbnail(0.5f)
                    .into(item_task_point_item_iv41);
        }else{
            item_task_point_item_iv41.setVisibility(GONE);
//            Glide.with(context)
//                    .load(pointBean.getImagePath1())
//                    .centerCrop()
//                    .thumbnail(0.5f)
//                    .into(item_task_point_item_iv41);
        }

        if(!TextUtils.isEmpty(pointBean.getImagePath2())){
            item_task_point_item_iv42.setVisibility(VISIBLE);
            Glide.with(context)
                    .load(pointBean.getImagePath2())
                    .centerCrop()
                    .thumbnail(0.5f)
                    .into(item_task_point_item_iv42);
        }else {
            item_task_point_item_iv42.setVisibility(GONE);
//            Glide.with(context)
//                    .load(pointBean.getImagePath2())
//                    .centerCrop()
//                    .thumbnail(0.5f)
//                    .into(item_task_point_item_iv42);
        }

        if(!TextUtils.isEmpty(pointBean.getImagePath3())){
            item_task_point_item_iv43.setVisibility(VISIBLE);
            item_task_point_item_iv4.setVisibility(GONE);
            Glide.with(context)
                    .load(pointBean.getImagePath3())
                    .centerCrop()
                    .thumbnail(0.5f)
                    .into(item_task_point_item_iv43);
        }else{
            item_task_point_item_iv43.setVisibility(GONE);
            item_task_point_item_iv4.setVisibility(VISIBLE);
//            Glide.with(context)
//                    .load(pointBean.getImagePath3())
//                    .centerCrop()
//                    .thumbnail(0.5f)
//                    .into(item_task_point_item_iv43);
        }

    }

    public void updateImageShow(int position){
        switch (position){
            case 0:
                item_task_point_item_iv41.setVisibility(VISIBLE);
                break;
            case 1:
                item_task_point_item_iv42.setVisibility(VISIBLE);
                break;
            case 2:
                item_task_point_item_iv43.setVisibility(VISIBLE);
                break;
        }

    }

    public interface ItemTaskPointListener {
        void takePhoto(int position,boolean isChecked);
    }

    private CloseListener closeListener;
    public void setCloseListener(CloseListener closeListener){
        this.closeListener = closeListener;
    }
    public interface CloseListener{
        void onClose(int state);
    }

    private CommitListener commitListener;
    public void setCommitListener(CommitListener commitListener){
        this.commitListener = commitListener;
    }
    public interface CommitListener{
        void onCommit();
    }

    private ImageDeleteListener imageDeleteListener;
    public void setImageDeleteListener(ImageDeleteListener imageDeleteListener){
        this.imageDeleteListener = imageDeleteListener;
    }
    public interface ImageDeleteListener{
        void onImageDelete(int index);
    }

    private ImageClickListener imageClickListener;
    public void setImageClickListener(ImageClickListener imageClickListener){
        this.imageClickListener = imageClickListener;
    }
    public interface ImageClickListener{
        void onClick(String path);
    }

}
